<template>
	<view class="login" v-if="loading">
		<view class="l-banner">
			<image class="image" src="http://klwl-file.oss-cn-shenzhen.aliyuncs.com/202105/jhstest/52839679236503.png"
				mode="widthFix"></image>
		</view>
		<view class="username mb-20" :class="{useractive:userblur}">
			<view class="iconfont iconfont-e619 mr-20"></view>
			<u-input class="input" type="text" v-model="dataForm.username" :clearable='false'
				:placeholder-style="userblur?'color:#49a3f1':''" placeholder="请输入账号" @focus="focus('user')"
				@blur="blur('user')" />
		</view>
		<view class="username" :class="{useractive:passblur}">
			<view class="iconfont iconfont-e619 mr-20"></view>
			<u-input class="input" type="password" v-model="dataForm.password" :clearable='false' :password-icon='false'
				:placeholder-style="passblur?'color:#49a3f1':''" maxlength='16' placeholder="请输入密码" @focus='focus' @blur="blur" />
		</view>
		<view class="login-btn" @click="login">登录</view>
		<view class="login-read">
			<view class="buy-icon" @click="change">
				<view class="iconfont iconfont-e720 " v-if="readbool==0"></view>
				<view class="iconfont iconfont-e721" v-else></view>
			</view>
			阅读并同意以下协议<text class="title" @click="openprop">《服务协议》</text>
		</view>
		<u-popup v-model="show" mode="center" closeable close-icon-pos='top-right' width='80%' border-radius='20'>
			<view class="prop-title">
				<text>服务协议</text>
			</view>
			<view class="prop-content">
				{{content}}
			</view>
			<view class="prop-btn" @click="show = false">我知道了</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		uniBack
	} from '@/utils/uniPromise.js'
	import {
		buildUrl
	} from './system.js'
	import {
		adminPost
	} from '@/common/http.request.js'
	export default {
		data() {
			return {
				userblur: false,
				passblur: false,
				show: false,
				content: '',
				loading: false,
				dataForm: {
					username: '',
					password: ''
				},
				readbool: 1,
				returnPage: '', // 登录后跳转的页面
			}
		},
		onLoad(options) {
			if (this.vuex_adminuser.port_type == 6 && this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/scenicSystem/home/index', 'redirectTo')
				return
			} else if (this.vuex_adminuser.port_type == 7 && this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/restaurantSystem/home/index', 'redirectTo')
			}
			else if (this.vuex_adminuser.port_type == 8 && this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/restaurantSystem/staff/index', 'redirectTo')
			}
			else if (this.vuex_adminuser.port_type == 4 && this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/supplierSide/index/index', 'redirectTo')
			}
			else if (this.vuex_adminuser.port_type == 9 && this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/hotelSystem/home/index', 'redirectTo')
			}
			else if (this.vuex_adminuser.port_type == 10&& this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/lineSystem/home/index', 'redirectTo')
			}else if (this.vuex_adminuser.port_type == 11&& this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/enroll/index', 'redirectTo')
			} else if (this.vuex_adminuser.pk_id) {
				this.$linkTo('/package/system/home/index', 'redirectTo')
			} else {
				this.loading = true
			}
			// if(options && options.returnPage){
			// 	this.returnPage = options.returnPage
			// }
		},
		methods: {
			focus(val) {
				if (val == 'user') {
					this.userblur = true
				} else {
					this.passblur = true
				}
			},
			blur(val) {
				if (val == 'user' && !this.dataForm.username) {
					this.userblur = false
				} else if (!val && !this.dataForm.password) {
					this.passblur = false
				}
			},
			change() {
				this.readbool = this.readbool == 0 ? 1 : 0
			},
			openprop() {
				this.show = true
			},
			login() {
				if (!this.dataForm.username) {
					return this.$toast('账号名不能为空')
				} else if (!this.dataForm.password) {
					return this.$toast('密码不能为空')
				} else if (this.readbool == 0) {
					return this.$toast('请阅读服务协议')
				}
				this.$loading()
				adminPost('GetManagerLogin', {
					login_url:this.vuex_businessObj.webUrl,
					...this.dataForm,
				}).then(res => {
					let url = '/package/system/home/index'
					let obj = res.data
					obj.optionId = obj.pk_id
					if(res.data.port_type == 6){/*景区管理*/
						url = '/package/scenicSystem/home/index'
						obj.fk_scenic_id = res.data.pk_id
						obj.pk_id = obj.fk_business_id
					}else if(res.data.port_type == 5){/*员工*/
						obj.fk_staff_id = res.data.pk_id
						obj.pk_id = obj.fk_business_id
					}else if(res.data.port_type == 7){/*餐饮*/
						url = '/package/restaurantSystem/home/index'
						obj.fk_meal_id = res.data.pk_id
						obj.pk_id = obj.fk_business_id
					}else if(res.data.port_type == 8){/*餐饮员工配送*/
						url = '/package/restaurantSystem/staff/index'
						obj.fk_meal_id = res.data.fk_meal_id //餐厅ID
						obj.staff_id = res.data.pk_id //员工ID
						obj.pk_id = obj.fk_business_id //商家ID
					}else if(res.data.port_type == 4){/*供应商端*/
						url = '/package/supplierSide/index/index'
						obj.supplier_id = res.data.pk_id //供应商ID
						obj.pk_id = obj.fk_business_id //商家ID
					}else if(res.data.port_type == 9){/*供应商端*/
						url = '/package/hotelSystem/home/index'
						obj.hotel_id = res.data.pk_id //酒店ID
						obj.pk_id = obj.fk_business_id //商家ID
					}else if(res.data.port_type == 10){/*线路*/
						url = '/package/lineSystem/home/index'
						obj.line_id = res.data.pk_id //线路ID
						obj.pk_id = obj.fk_business_id //商家ID
					}else if(res.data.port_type == 11){/*学校机构*/
						this.$toast('机构端暂未开放')
						return
						// url = '/package/enroll/index'
						// obj.enroll_id = res.data.pk_id //学校机构ID
						// obj.pk_id = obj.fk_business_id //商家ID
					}
					

					
					if (obj.pk_id !== this.vuex_business.pk_id) {
						this.$toast('没有查询到账号信息')
						return
					}
					this.$u.vuex('vuex_adminuser', obj)
					this.$hide()
					this.$linkTo(url, 'redirectTo')
				}).catch(()=>{
					this.$hide()
				})
			},
			JumpPage() {
				if (this.returnPage) {
					let returnPage = JSON.parse(this.returnPage)
					let query = returnPage.query || {}
					let path = returnPage.path
					let returnUrl = buildUrl(path, query)
					this.$linkTo(returnUrl, 'redirectTo')
				} else {
					uniBack()
				}
			}
		}
	}
</script>

<style lang='scss' scoped>
	.login {
		padding: 0 120rpx;

		/* height: 100vh; */
		.l-banner {
			width: 100%;
			display: flex;
			justify-content: center;
			padding-top: 150rpx;
			margin-bottom: 80rpx;

			.image {
				width: 155rpx;
			}
		}

		.username {
			padding-bottom: 45rpx;
			border-bottom: 1px solid #e5e5e5;
			display: flex;

			.iconfont {
				margin-right: 20rpx;
			}

			.input {
				flex: 1;
				overflow: hidden;
			}
		}

		.mb-20 {
			margin-bottom: 45rpx;
		}

		.mr-20 {
			margin-right: 20rpx;
		}

		.login-btn {
			margin-top: 110rpx;
			margin-bottom: 30rpx;
			width: 100%;
			background-color: #49a3f1;
			color: #fff;
			text-align: center;
			line-height: 90rpx;
			border-radius: 20rpx;
		}

		.login-read {
			display: flex;
			align-items: center;
			color: #999;
			padding: 0 0 0 30rpx;

			.buy-icon {
				margin-right: 18rpx;
				color: #49a3f1;
				font-size: 34rpx;
			}

			.title {
				color: #49a3f1;
			}
		}

		.useractive {
			color: #49a3f1;
			border-color: #49a3f1;
		}

		.prop-title {
			display: flex;
			justify-content: center;
			align-items: center;
			line-height: 90rpx;
			border-bottom: 1px solid #e5e5e5;
		}

		.prop-content {
			height: 450rpx;
		}

		.prop-btn {
			border-top: 1px solid #e5e5e5;
			color: #49a3f1;
			display: flex;
			justify-content: center;
			align-items: center;
			line-height: 80rpx;
		}
	}
</style>
